//id ,是否折叠其他,默认显示第几个 0开始
var water1, water2;
sui.ready(function() {
	//sui.accordion('#pannel1', false, 0);
	//sui.accordion('#pannel2', false, 1);
	water1 = document.querySelector('#water1');
	water2 = document.querySelector('#water2');
	//console.info(water1);
	//setDate();
	setSvg();
	//loadDevice();
	//beginAnimation();

})

function setDate() {
	sui("#data").html(getDateBegin());
	sui("#weekend").html(getWeekend());
}


async function beginAnimation(time) {
	await waitingS(1);
	water1.className.baseVal = "water";
	await waitingS(4);
	water1.className.baseVal = "run_water";
	await waitingS(6);
	water1.className.baseVal = "no-water";
	water2.className.baseVal = "water";
	await waitingS(4);
	water2.className.baseVal = "run_water";
	//water2.className.baseVal = "water";
}

async function waitingS(time) {
	return new Promise(function(resolve, reject) {
		setTimeout(function() {
			resolve("成功");
		}, time * 1000);
	});
}

function setSvg() {
	var clientWidth = document.documentElement.clientWidth,
		clientHeight = document.documentElement.clientHeight;

	console.log("clientWidth:" + clientWidth);
	console.log("clientHeight:" + clientHeight);

	var x = 1200;
	var y = 900;
	var svg = document.querySelector('#svg_box');
	var viewBoxVal = svg.getAttribute("viewBox");
	console.log("viewBoxVal:" + viewBoxVal);
	var viewBoxWidth = viewBoxVal.split(",")[2];
	var viewBoxHeight = viewBoxVal.split(",")[3];

	console.log("viewBoxWidth:" + viewBoxWidth);
	console.log("viewBoxHeight:" + viewBoxHeight);

	var setWidth = clientWidth * 0.8;
	var setHeight = setWidth * viewBoxHeight / viewBoxWidth;

	console.log("viewBoxWidth:" + setWidth);
	console.log("viewBoxHeight:" + setHeight);
	svg.setAttribute("width", setWidth);
	svg.setAttribute("height", setHeight);
}


function getWeekend() {
	var mydate = new Date();
	var myddy = mydate.getDay(); //获取存储当前日期
	var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
	return weekday[myddy];
}

function openControl() {
	sui.open("control.html", {}, {}, true, {})
}


// 关闭程序（js调用后台方法）
function closeWindow() {
	jsObjAsync.closeWindow();
}

//获取详细信息
function loadDevice() {
	// jsObjAsync.loadDevice().then(function(data) {
	// 	refreshDevice(data);
	// })
	try {
		jsObjAsync.loadDevice().then(function(data) {
			refreshDevice(data);
		});
	} catch (e) {
		apiManage.get("./bak/data.json", null, function(ret) {
			console.log(JSON.stringify(ret));
			//_self.detailList = ret.Data;	
			refreshDevice(ret);
		}, function(err) {});
	}
}

//后台调用刷新
function refreshDevice(data) {
	vm.detailList = data.Data;
	vm.refreshView();
}


var vm = new Vue({
	el: "#datas",
	data: {
		detailList: []
	},
	mounted: function() {
		loadDevice();
	},
	methods: {
		getBen: function(groupName, targetName) {
				//1#泵站 1#泵电压_1 ,
			var group = null;
			var list = this.detailList;
			for (var i = 0; i < list.length; i++) {
				//console.log( list[i].name + "|" + groupName);
				if (list[i].name == groupName) {
					group = list[i];
				}	
			}
		
			var retStr ="";
			if (group){
				for (var i = 0; i < group.targets.length; i++) {
					//console.log( group.targets[i].name + "|" + targetName);
					if (  group.targets[i].name == targetName) {
						
						DebugPrint({
							"grouptargets": group.targets[i]
						})
						
						retStr = group.targets[i].value + " " + group.targets[i].unit
					}
				
				}
			}
			

			return retStr;

		},
		
		
		refreshView:function(){
			console.log("------更新svg数据-----");
			
			this.setText("csyl1","1#泵站","出水压力_1");
			this.setText("csyl2","2#泵站","出水压力_2");
			this.setText("csyl3","3#泵站","出水压力_3");
			
			
			this.setText("zll1","1#泵站","瞬时流量_1");
			this.setText("zll2","2#泵站","瞬时流量_2");
			this.setText("zll3","3#泵站","瞬时流量_3");
			
			
			this.setText("yw1","1#泵站","水箱的实际液位_1"," ");
			this.setText("yw2","2#泵站","水箱的实际液位_2"," ");
			
			this.setText("rsll","泵房监控","出水管道瞬时流量");
			this.setText("yl","泵房监控","进水压力");
			
			// var zll1 =  this.getBen("1#泵站","累计流量_1");
			// document.getElementById("zll1").textContent = "累计流量：" + zll1;
			
			
		},
		
		
		setText:function(id,group,name,title){
			//var csyl1 =  this.getBen("1#泵站","出水压力_1");
			//document.getElementById("csyl1").textContent = "出水压力：" + csyl1;
			
			var str =  this.getBen(group,name);
			title= title || name.split("_")[0] + "：";
			document.getElementById(id).textContent = title  + str;
		}



	}



})
